package com.cloudfast.oauth.principal;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudfast.common.user.LoginUser;
import com.cloudfast.platform.menu.entity.Menu;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

@SuppressWarnings("serial")
public class Principal extends User {

    private Map<String, Object> cache;

    public Principal(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        cache = new HashMap<String, Object>();
    }

    public Principal(LoginUser user, String password, Collection<? extends GrantedAuthority> authorities) {
        super(user.getUsername(), password, authorities);
        cache = new HashMap<String, Object>();
        cache.put("user", user);
    }

    public Principal(LoginUser user, Collection<? extends GrantedAuthority> authorities) {
        super(user.getUsername(), user.getPassword(), authorities);
        cache = new HashMap<>();
        cache.put("user", user);
    }

    /**
     *
     * @param user 登入的用户
     * @param authorities 权限信息
     * @param systemMenus       拥有的System菜域单
     * @param portalMenus       拥有的Portal菜域单
     * @param screenBtn   大屏按钮入口
     */
    public Principal(LoginUser user,
                     Collection<? extends GrantedAuthority> authorities,
                     List<Map<String, Object>> systemMenus,
                     List<Map<String, Object>> portalMenus,
                     List<Menu> screenBtn) {
        super(user.getUsername(), user.getPassword(), authorities);
        cache = new HashMap<>();
        cache.put("user", user);
        cache.put("system_menus", systemMenus);
        cache.put("portal_menus", portalMenus);
        cache.put("screenBtn", screenBtn);
    }
    public Map<String, Object> getCache() {
        if (cache == null) {
            cache = new HashMap<>();
        }
        return cache;
    }

}
